# coding=UTF-8
import image_handler
import mapper_word


# 4个区间比例问题
def style_one(img):
    interval_1_black = interval_1_white = interval_2_black = interval_2_white \
        = interval_3_black = interval_3_white = interval_4_black = interval_4_white = 0
    length = img.size[0]
    width = img.size[1]
    horizontal_array = image_handler.ImageHandler.get_img_horizontal_array(img, 222)
    for x in range(0, width):
        for y in range(0, length):
            if horizontal_array[x][y] == '(0, 0, 0)':
                black = 1
                white = 0
            else:
                black = 0
                white = 1
            if 0 <= x <= width / 2 and 0 <= y <= length / 2:
                interval_2_black += black
                interval_2_white += white
            elif width / 2 <= x <= width and 0 <= y <= length / 2:
                interval_3_black += black
                interval_3_white += white
            elif 0 <= x <= width / 2 and length / 2 <= y <= length:
                interval_1_black += black
                interval_1_white += white
            elif width / 2 <= x <= width and length / 2 <= y <= length:
                interval_4_black += black
                interval_4_white += white
    interval_1_scale = float(interval_1_black) / float(interval_1_white)
    interval_2_scale = float(interval_2_black) / float(interval_2_white)
    interval_3_scale = float(interval_3_black) / float(interval_3_white)
    interval_4_scale = float(interval_4_black) / float(interval_4_white)
    interval_1 = {"scale": '%.5f' % interval_1_scale}
    interval_2 = {"scale": '%.5f' % interval_2_scale}
    interval_3 = {"scale": '%.5f' % interval_3_scale}
    interval_4 = {"scale": '%.5f' % interval_4_scale}
    return interval_1, interval_2, interval_3, interval_4

# 特征：判断文字笔画
def style_two(img):
    pass


def check_punctuation_radical(img):
    a, b, c, d = style_one(img)
    scale_str = (a['scale'], b['scale'], c['scale'], d['scale'])
    if scale_str in mapper_word.style_one_mapper.keys():
        return 1
    elif scale_str in mapper_word.style_one_mapper_radical.keys():
        return 2
    else:
        return 0


def mapper_word_self(img):
    a, b, c, d = style_one(img)
    scale_str = (a['scale'], b['scale'], c['scale'], d['scale'])
    if scale_str in mapper_word.style_one_mapper.keys():
        word = mapper_word.style_one_mapper[scale_str]
    else:
        word = ''
    return {
        'style_one': scale_str,
        'word': word
    }


def to_write(param, img):
    box_point = param
    save_img = img.crop(box_point)
    word_map = mapper_word_self(save_img)
    if word_map['word'] == '':
        save_img.save('img/' + str(box_point) + ' | ' + str(word_map['style_one']) + '.png')
        return '?'
    else:
        return word_map['word']


def calculate(img, horizontal_array, vertical_array, word_size):
    result = ''
    old = ()
    pixel = word_size * 98 / 72
    x1 = x2 = y1 = y2 = 0
    x = y = 0
    width = img.size[1]
    length = img.size[0]
    while x <= width:
        val_x = x

        for i in range(x, len(horizontal_array)):
            if '(0, 0, 0)' in horizontal_array[i][0: length]:
                x1 = i
                break
            x = i + 1

        for i in range(x, len(horizontal_array)):
            if '(0, 0, 0)' not in horizontal_array[i][0: length]:
                x2 = i
                break
            x = i + 1

        while y <= length:
            val_y = y
            for j in range(y, len(vertical_array)):
                if '(0, 0, 0)' in vertical_array[j][x1: x2]:
                    y1 = j
                    break
                y = j + 1

            for j in range(y, len(vertical_array)):
                if '(0, 0, 0)' not in vertical_array[j][x1: x2]:
                    y2 = j
                    break
                y = j + 1

            if old == (y1, x1, y2, x2):
                pass
            else:
                old = (y1, x1, y2, x2)
                # 判断是否为偏旁
                save_img = img.crop((y1, x1, y2, x2))
                if check_punctuation_radical(save_img) == 0:
                    result += to_write((y1, x1, y2, x2), img)
                elif check_punctuation_radical(save_img) == 1:
                    result += to_write((y1, x1, y2, x2), img)
                elif check_punctuation_radical(save_img) == 2:
                    # 当读取到偏旁时,继续找下个节点
                    for j in range(y, len(vertical_array)):
                        if '(0, 0, 0)' in vertical_array[j][x1: x2]:
                            break
                        y = j + 1

                    for j in range(y, len(vertical_array)):
                        if '(0, 0, 0)' not in vertical_array[j][x1: x2]:
                            y2 = j
                            break
                        y = j + 1
                    result += to_write((y1, x1, y2, x2), img)
                else:
                    result += to_write((y1, x1, y2, x2), img)
            if val_y == y or y >= len(vertical_array):
                break

        y = 0
        if val_x == x or x >= len(horizontal_array):
            break
    print result
